﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MvcSmartTool.Common {
    /// <summary>
    /// Defines the type that will be generated.
    /// </summary>
    public class TypeGenerationModel : BasePropertyChanged {
        /// <summary>
        /// Gets the namespace of the generated view model.
        /// </summary>
        public string Namespace { get; private set; }

        /// <summary>
        /// Gets the name of the generated view model for which to generate the view model.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the type of the data model.
        /// </summary>
        public Type ModelType { get; private set; }
        
        /// <summary>
        /// Gets the type name of the data model.
        /// </summary>
        public string ModelTypeName { get; private set; }

        /// <summary>
        /// Gets the properties to generate.
        /// </summary>
        public IEnumerable<PropertyGenerationModel> Properties { get; private set; }

        public IEnumerable<PropertyGenerationModel> VirtualProperties { get; private set; } 
        public IList<string> ForeignKeys {
            get { return VirtualProperties.Select(x => x.Name + "Id").ToList(); }
        }

        public string ModelId {
            get {
                return Name + "Id";
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeGenerationModel"/> class.
        /// </summary>
        /// <param name="modelType">Type of the data model for which to generate the view model.</param>
        /// <param name="namespace">The @namespace of the generated view model.</param>
        public TypeGenerationModel(Type modelType, string @namespace) {
            Namespace = @namespace;
            ModelType = modelType;
            ModelTypeName = modelType.GetTypeName();
            Name = modelType.Name;
            var allProperties =ModelType
                        .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                        .Select(p => new PropertyGenerationModel(this, p))
                        .Where(p => p.CanGet || p.CanSet)
                        .ToList();

            Properties = allProperties.Where(x => !x.IsVirtual);
            VirtualProperties = allProperties.Where(x => x.IsVirtual);
        }
    }
   
}
